package com.xuxueli.applyModules.utils.testtree;

import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.BeanUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 测试树形结果
 */
public class TestTree {
    public static void main(String[] args) {
        List<WarningLabelDetailDto> parentList = new ArrayList<>();
        //List<WarningLabelDetailDto> parentList = toVO(this.selectList(null));
        List<WarningLabelDetailDto> childList = new ArrayList<>();
        //List<WarningLabelDetailDto> childList = toVO( this.selectList(null));
        if(CollectionUtils.isNotEmpty(parentList)){
            parentList.forEach(parent -> listChildren(parent, childList));
        }
    }

    private static void listChildren(WarningLabelDetailDto parent, List<WarningLabelDetailDto> allList) {
        List<WarningLabelDetailDto> childCallers = allList.stream() .filter(child -> child.getpCode() != null && child.getpCode().equals(parent.getCode())) .collect(Collectors.toList());
        if (!childCallers.isEmpty()) {
            childCallers.forEach(child -> {
                listChildren(child, allList);
                parent.getChrildList().add(child);
            });
        }
    }

    /**
     * 根据树形展开所有
     */
    public static List<WarningLabelDetailView> getAllList(List<WarningLabelDetailDto> warningLabelDetailDtos){
        List<WarningLabelDetailView> list = new ArrayList<>();
        warningLabelDetailDtos.stream().forEach(item->{ convertList(item,list); });
        return list;
    }

    private static void convertList(WarningLabelDetailDto parent, List<WarningLabelDetailView> list) {
        if(null!=parent){
            WarningLabelDetailView warningLabelDetailView = new WarningLabelDetailView();
            BeanUtils.copyProperties(parent,warningLabelDetailView);
            list.add(warningLabelDetailView);
            List<WarningLabelDetailDto> chrildList = parent.getChrildList();
            if(CollectionUtils.isNotEmpty(chrildList)){
                chrildList.stream().forEach(child->{ convertList(child,list); });
            }
        }
    }
}
